/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
package com.imesne.office.excel.html;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.ArrayList;
import java.util.List;

/**
 * Implementation of {@link HtmlHelper} for HSSF files.
 */
public class HSSFHtmlHelper extends AbstractHtmlHelper {

    private static final HSSFColor HSSF_AUTO = HSSFColorPredefined.AUTOMATIC.getColor();

    public List<String> getStyles(Workbook workbook, CellStyle style) {
        List<String> colorStyleList = new ArrayList<>();
        HSSFCellStyle cs = (HSSFCellStyle) style;

        HSSFPalette colors = ((HSSFWorkbook) workbook).getCustomPalette();
        styleColor(colors, "background-color", cs.getFillForegroundColor(), colorStyleList);
        styleColor(colors, "color", cs.getFont(workbook).getColor(), colorStyleList);

        addBorderStyle(colors, "border-left", cs.getBorderLeftEnum(), cs.getLeftBorderColor(), colorStyleList);
        addBorderStyle(colors, "border-right", cs.getBorderRightEnum(), cs.getRightBorderColor(), colorStyleList);
        addBorderStyle(colors, "border-top", cs.getBorderTopEnum(), cs.getTopBorderColor(), colorStyleList);
        addBorderStyle(colors, "border-bottom", cs.getBorderBottomEnum(), cs.getBottomBorderColor(), colorStyleList);

        return colorStyleList;
    }

    protected void addBorderStyle(HSSFPalette colors, String attr, BorderStyle borderStyle, short borderColorIndex, List<String> cellStyles) {

        String value = BORDER.get(borderStyle);
        if (value == null) {
            return;
        }
        String color = "black";
        HSSFColor hssfColor = colors.getColor(borderColorIndex);
        if (borderColorIndex != HSSF_AUTO.getIndex() && hssfColor != null) {
            short[] rgb = hssfColor.getTriplet();
            color = String.format("#%02x%02x%02x", rgb[0], rgb[1], rgb[2]);
        }
        String border = String.format(value, color);
        cellStyles.add(String.format("%s: %s;", attr, border));
    }

    private void styleColor(HSSFPalette colors, String attr, short index, List<String> colorStyleList) {

        HSSFColor color = colors.getColor(index);
        if (index == HSSF_AUTO.getIndex() || color == null) {
            return;
        }
        short[] rgb = color.getTriplet();
        colorStyleList.add(String.format("  %s: #%02x%02x%02x;%n", attr, rgb[0], rgb[1], rgb[2]));
    }
}